﻿using iSchool.FinanceCenter.Domain.Entities;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;

namespace DapperLinq.Helper
{
    public class WalletHelper
    {
        /// <summary>
        /// 查询数据生成用户信息认证密钥
        /// </summary>
        /// <param name="data"></param>
        /// <param name="json"></param>
        /// <returns></returns>
        public string CheckSign(wallet data)
        {
            var paramJson = new
            {
                userId = data.UserId,
                totalAmount = (int)(data.TotalAmount * 10000),
                blockedAmount = (int)(data.BlockedAmount * 10000),
                remainAmount = (int)(data.RemainAmount * 10000),
                updateTime = data.UpdateTime.ToString("yyyy-MM-dd HH:mm:ss"),
                virtualTotalAmount = (int)(data.VirtualTotalAmount * 10000),
                virtualRemainAmount = (int)(data.VirtualRemainAmount * 10000)
            };
            var json = JsonConvert.SerializeObject(paramJson).Trim();
            var paramMd5 = MD5.Compute(json).ToLowerInvariant();
            return paramMd5;
        }


        public static class MD5
        {
            public static string Compute(string data)
            {
                if (string.IsNullOrEmpty(data))
                {
                    throw new ArgumentNullException(nameof(data));
                }

                using (var md5 = System.Security.Cryptography.MD5.Create())
                {
                    var hsah = md5.ComputeHash(Encoding.UTF8.GetBytes(data));
                    return BitConverter.ToString(hsah).Replace("-", "");
                }
            }
        }
    }
}
